﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entity;
using System.Data.SqlClient;
using System.Data;
using System.Data.Sql;
using System.Data.SqlTypes;

namespace DAL
{
   public class UsuarioWrapper : BaseWrapper, IWrapper<Usuario>
    {

        public List<Usuario> GetAll()
        {
            List<Usuario> LISTA = new List<Usuario>();

            SqlConnection cn = new SqlConnection(ConnectionString());
            SqlCommand cmd = new SqlCommand("SP_USUARIO_GETALL", cn);
            cmd.CommandType = CommandType.StoredProcedure;

  
            try
            {
                cn.Open();
                SqlDataReader dr = cmd.ExecuteReader();

                while (dr.Read())
                {
                    Usuario User = new Usuario();
                    User.IDUSUARIO = int.Parse(dr["IDUSUARIO"].ToString());
                    User.EMAIL = dr["EMAIL"].ToString();
                    User.SENHA = dr["SENHA"].ToString();
                    User.TIPOUSUARIO = bool.Parse(dr["TIPOUSUARIO"].ToString());
                    LISTA.Add(User);
                }
            }
            catch (SqlException ex)
            {
                Messages += "Erro na inserção de dados\n";
                Messages += ex.Message.ToString();
                Logs.MENSAGEM = Messages;
                Logs.TIPOLOG = "Leitura de dados";
                LOGsWrapper LogsWrapper = new LOGsWrapper();
                LogsWrapper.Insert(Logs);
            }
            finally
            {
                ;
                cn.Close();
            }
            return LISTA;
                    

        }

        public Usuario GetById(int id)
        {
            Usuario User = new Usuario();

            SqlConnection cn = new SqlConnection(ConnectionString());
            SqlCommand cmd = new SqlCommand("SP_USUARIO_GETBYID", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@IDUSUARIO", id);

  
            try
            {
                cn.Open();
                SqlDataReader dr = cmd.ExecuteReader();

                while (dr.Read())
                {
                    
                    User.IDUSUARIO = int.Parse(dr["IDUSUARIO"].ToString());
                    User.EMAIL = dr["EMAIL"].ToString();
                    User.SENHA = dr["SENHA"].ToString();
                    User.TIPOUSUARIO = bool.Parse(dr["TIPOUSUARIO"].ToString());
                    
                }
            }
            catch (SqlException ex)
            {
                Messages += "Erro na inserção de dados\n";
                Messages += ex.Message.ToString();
                Logs.MENSAGEM = Messages;
                Logs.TIPOLOG = "Leitura de dados";
                LOGsWrapper LogsWrapper = new LOGsWrapper();
                LogsWrapper.Insert(Logs);
            }
            finally
            {
     
                cn.Close();
            }
            return User;
        }

        public bool Insert(Usuario entity)
        {
            bool status = false;
            SqlConnection cn = new SqlConnection(ConnectionString());
            SqlCommand cmd = new SqlCommand("SP_USUARIO_INSERT", cn);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            
            SqlParameter parametro = new SqlParameter("@IDUSUARIO",DbType.UInt32);
            parametro.Direction = ParameterDirection.Output;

            cmd.Parameters.Add(parametro);

         
            cmd.Parameters.AddWithValue("@EMAIL", entity.EMAIL);
            cmd.Parameters.AddWithValue("@SENHA", entity.SENHA);
            cmd.Parameters.AddWithValue("@TIPOUSUARIO", entity.TIPOUSUARIO);

            try
            {
                cn.Open();
                cmd.ExecuteNonQuery();
                entity.IDUSUARIO = int.Parse(cmd.Parameters[0].Value.ToString());

                    status = true;
            }
            catch (SqlException ex)
            {
                Messages += "Erro na inserção de dados\n";
                Messages += ex.Message.ToString();
                Logs.MENSAGEM = Messages;
                Logs.TIPOLOG = "Manipulação de dados";
                LOGsWrapper LogsWrapper = new LOGsWrapper();
                LogsWrapper.Insert(Logs);

            }
            finally
            {
                if (cn.State != ConnectionState.Closed)
                    cn.Close();
            }

            return status;

        }

        public bool Update(Usuario entity)
        {
            bool status = false;
            SqlConnection cn = new SqlConnection(ConnectionString());
            SqlCommand cmd = new SqlCommand("SP_USUARIO_UPDATE", cn);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@IDUSUARIO", entity.IDUSUARIO);
            cmd.Parameters.AddWithValue("@EMAIL", entity.EMAIL);
            cmd.Parameters.AddWithValue("@SENHA", entity.SENHA);
            cmd.Parameters.AddWithValue("@TIPOUSUARIO", entity.TIPOUSUARIO);

            try
            {
                cn.Open();
                cmd.ExecuteNonQuery();
                status = true;
            }
            catch (SqlException ex)
            {
                Messages += "Erro na atualização dos dados\n";
                Messages += ex.Message.ToString();
                Logs.MENSAGEM = Messages;
                Logs.TIPOLOG = "Manipulação de dados";
                LOGsWrapper LogsWrapper = new LOGsWrapper();
                LogsWrapper.Insert(Logs);
            }
            finally
            {
                if (cn.State != ConnectionState.Closed)
                    cn.Close();
            }

            return status;
        }

        public bool Delete(Usuario entity)
        {
            bool status = false;
            SqlConnection cn = new SqlConnection(ConnectionString());
            SqlCommand cmd = new SqlCommand("SP_USUARIO_DELETE", cn);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@IDUSUARIO", entity.IDUSUARIO);

            try
            {
                cn.Open();
                cmd.ExecuteNonQuery();
                status = true;
            }
            catch (SqlException ex)
            {
                Messages += "Erro na exclusão dos dados\n";
                Messages += ex.Message.ToString();
                Logs.MENSAGEM = Messages;
                Logs.TIPOLOG = "Manipulação de dados";
                LOGsWrapper LogsWrapper = new LOGsWrapper();
                LogsWrapper.Insert(Logs);
            }
            finally
            {
                if (cn.State != ConnectionState.Closed)
                    cn.Close();
            }

            return status;

        }
    }
}
